//
//  AuthBankCardContentView.swift
//  认证 - 银行卡内容 View
//
//  Created by 千策万划-mini on 2025/5/7.
//

import Foundation

class AuthBankCardContentView: UIView {

    var modelDetail: ModelBankCardInfo = .init()
    var blockUpdataModel: ((ModelBankCardInfo) -> Void)?
    var rag: Int = 0

    // MARK: - lazy
    lazy var imageScheduleBg: UIImageView = {
        let result = UIImageView()
        result.image = UIImage(named: "icon_auth_schedule_bg")
        result.widthHeight = XY(SCREEN_WIDTH - W(15) * 2, W(68))
        return result
    }()

    lazy var imageSchedule: UIImageView = {
        let result = UIImageView()
        result.widthHeight = XY(W(329), W(38))
        return result
    }()

    lazy var viewContentBack: UIView = {
        let result = UIView()
        result.isUserInteractionEnabled = true
        result.backgroundColor = UIColor(hex: "#BEAAFF")
        GlobalMethod.setRoundView(view: result, color: UIColor(hex: "#7B51FF"), numRound: 16, width: 4)
        result.width = SCREEN_WIDTH - W(15) * 2
        return result
    }()

    lazy var line: UIView = {
        let result = UIView()
        result.height = 2
        result.backgroundColor = UIColor(hex: "#36188A")
        return result
    }()

    lazy var viewContent: UIView = {
        let result = UIView()
        result.backgroundColor = .clear
        result.width = viewContentBack.width
        return result
    }()
    
    
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        widthHeight = XY(SCREEN_WIDTH, 0)
        backgroundColor = .clear

        addSubView()
    }

    @available(*, unavailable)
    required init?(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - 添加
    func addSubView() {
        addSubview(imageScheduleBg)
        imageScheduleBg.addSubview(imageSchedule)
        addSubview(viewContentBack)
        viewContentBack.addSubview(line)
        viewContentBack.addSubview(viewContent)
        resetView()
    }

    // MARK: - 点击事件
    @objc func btnTypeClick(_ sender: UIControl) {
        feedback()
        sender.addClickAnimate()
        for subview in viewContentBack.subviews {
            if let btn = subview as? UIButton, btn.tag >= 100, btn.tag < 150 {
                btn.isSelected = (btn == sender)
                // 更新 label 样式
                if let label = btn.subviews.first(where: { $0 is OutlineLabel }) as? OutlineLabel {
                    label.resetView(
                        title: UnPackStr(modelDetail.putting[btn.tag - 100].whose),
                        textColor: COLOR_WHITE(),
                        font: btn.isSelected ? UIFont.customExtraBoldFont(size: 14) : UIFont.customMediumFont(size: 14),
                        shadowColor: UIColor(hex: btn.isSelected ? "#B05703" : "#2D0084"),
                        outlineColor: UIColor(hex: btn.isSelected ? "#E28B0A" : "#2D0084")
                    )
                }
            }
        }
        resetContent(index: sender.tag-100)
        rag = sender.tag - 100
    }

    // MARK: - 重置界面
    func resetView(model: ModelBankCardInfo = ModelBankCardInfo()) {
        modelDetail = model
        imageScheduleBg.centerXTop = XY(SCREEN_WIDTH / 2, W(16))
        let count = GlobalMethod.readInt(forKey: LOCAL_AUTH_PRODUCT_COUNT)
        imageSchedule.image = UIImage(named: count == 5 ? "icon_auth_schedule_5_5" : "icon_auth_schedule_4_4")
        imageSchedule.centerXCenterY = XY(imageScheduleBg.width / 2, imageScheduleBg.height / 2)

        if isAry(model.putting) {
            for subview in viewContentBack.subviews {
                if subview.tag >= 100 {
                    subview.removeFromSuperview()
                }
            }
            let widthBtn = W(100)
            let interval = CGFloat(model.putting.count - 1) * W(10)
            let marginBtn = (viewContentBack.width - (CGFloat(model.putting.count) * widthBtn + interval)) / 2

            for i in 0 ..< model.putting.count {
                let btn = UIButton()
                btn.widthHeight = XY(widthBtn, W(48))
                btn.leftTop = XY(marginBtn + (widthBtn + W(10)) * CGFloat(i), W(13))
                btn.setBackgroundImage(UIImage(named: "icon_order_type_normal"), for: .normal)
                btn.setBackgroundImage(UIImage(named: "icon_order_type_selected"), for: .selected)
                btn.tag = 100 + i
                btn.isSelected = i == 0
                btn.addTarget(self, action: #selector(btnTypeClick(_:)), for: .touchUpInside)
                
                let label = OutlineLabel()
                label.resetView(
                    title: UnPackStr(model.putting[i].whose),
                    textColor: COLOR_WHITE(),
                    font: i == 0 ? UIFont.customExtraBoldFont(size: 14) : UIFont.customMediumFont(size: 14),
                    shadowColor: UIColor(hex: i == 0 ? "#B05703" : "#2D0084"),
                    outlineColor: UIColor(hex: i == 0 ? "#E28B0A" : "#2D0084")
                )
                label.centerXCenterY = XY(btn.width / 2, btn.height / 2)
                btn.addSubview(label)

                viewContentBack.addSubview(btn)
            }
        }

        line.width = viewContentBack.width - 4 * 2
        line.centerXTop = XY(viewContentBack.width / 2, W(12) + W(48) + W(10))
        
        if(isAry(model.putting)){
            resetContent(index: 0)
        }
    }
    
    // 切换类型 - 显示下发输入框内容
    func resetContent(index: Int = 0) {
        var topMark = 0.0
        if isAry(modelDetail.putting[index].putting){
            viewContent.removeAllSubviews()
            for i in 0..<modelDetail.putting[index].putting.count {
                let modelItem = modelDetail.putting[index].putting[i]
                if modelItem.imp == "Brown" {
                    // 输入框
                    let textFieldView = AuthTFView()
                    textFieldView.resetView(model: modelItem)
                    textFieldView.blockValueChange = { [weak self](model) in
                        guard let self = self else { return }
                        self.modelDetail.putting[index].putting[i] = model
                        if let block = blockUpdataModel {
                            block(self.modelDetail)
                        }
                    }
                    textFieldView.centerXTop = XY(viewContentBack.width/2, topMark);
                    viewContent.addSubview(textFieldView)
                    topMark = textFieldView.bottom
                }
                if modelItem.imp == "hurried" {
                    // 单选
                    let singleChoiceView = SingleChoiceView()
                    singleChoiceView.resetView(model: modelItem)
                    singleChoiceView.pickerView.titleStr = "Select \(modelDetail.putting[index].whose == "Bank" ? "Bank Name" : "Cash Name")"
                    singleChoiceView.pickerView.blockConfirmClick = { [weak self](selectValue, selectCode) in
                        guard let self = self else { return }
                        self.modelDetail.putting[index].putting[i].pounding = selectValue
                        self.modelDetail.putting[index].putting[i].wisest = selectCode
                        singleChoiceView.showContent(content: selectValue)
                        if let block = blockUpdataModel {
                            block(self.modelDetail)
                        }
                    }
                    singleChoiceView.centerXTop = XY(viewContentBack.width/2, topMark);
                    viewContent.addSubview(singleChoiceView)
                    topMark = singleChoiceView.bottom
                }
            }
            viewContent.height = topMark + W(20)
            viewContent.centerXTop = XY(viewContentBack.width/2, line.bottom+1)
            
            viewContentBack.height = viewContent.bottom
            viewContentBack.centerXTop = XY(SCREEN_WIDTH / 2, imageScheduleBg.bottom + W(16))

            height = viewContentBack.bottom + getBottomSafeAreaHeight() + 60 + W(44) + 10
        }
        
    }
}
